package xorm

import(
	"my_server_test/databest"
	//"my_server_test/model"
	//"fmt"

	_ "github.com/go-sql-driver/mysql"
	"github.com/go-xorm/xorm"
)

func SetXorm(masterDsn string, slaveDsn string){
	var err error
	databest.Engine, err = ConnectDbsLink(masterDsn, slaveDsn)
	if err != nil {
		panic(err)
	}
	// 关于配置表使用 xorm 同步机制，并且加入缓存
	err = databest.Engine.Sync2()
	if err != nil {
		panic(err)
	}
}
// ConnectDb 连接数据库，主从
func ConnectDbsLink(masterDsn string, slaveDsn string) (*xorm.EngineGroup, error){
	conns := []string{
		masterDsn,
		slaveDsn,
	}

	engineGroup, err := xorm.NewEngineGroup("mysql", conns)
	if err != nil {
		return nil, err
	}
	// engineGroup.SetLogger(model.Log)
	engineGroup.ShowSQL(true)

	engineGroup.SetMaxIdleConns(150)
	engineGroup.SetMaxOpenConns(1000)

	// cacher := xorm.NewLRUCacher(xorm.NewMemoryStore(), 10000)
	// engineGroup.MapCacher(&model.ActivityGroup{}, cacher)
	// engineGroup.MapCacher(&model.ActivityItem{}, cacher)

	// engineGroup.MapCacher(&model.Game{}, cacher)
	return engineGroup, nil
}